CheckSavePerms.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. %=================================================================%
  2. % Checks status & permissions of specified directory / file. %
  3. % Similar to CheckSavePath except existing files are NOT removed. %
  4. % %
  5. % Note: This is for cases where "cfg.outputfile" is used. %
  6. % Checks output directory without removing any existing files. %
  7. % Last modified: Sept. 7, 2014 %
  8. %=================================================================%
  9. % Copyright (C) 2013-2014, Michael J. Cheung
  10. %
  11. % This file is a part of the MEG & PLS Pipeline (MEGPLS). For more
  12. % details, see the documentation included with the software package.
  13. %
  14. % MEGPLS is free software: you can redistribute it and/or modify it under
  15. % the terms of the GNU General Public License version 2 as published by
  16. % the Free Software Foundation. This program is distributed in the hope
  17. % that it will be useful, but WITHOUT ANY WARRANTY; without even the
  18. % implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. % See the GNU General Public License for more details.
  20. %
  21. % You should have received a copy of the GNU General Public License along
  22. % with this program. If not, you can download the license here:
  23. % <http://www.gnu.org/licenses/old-licenses/gpl-2.0>.
  24. function CheckSavePerms(OutputPath, ErrLogName)
  25. SaveErrLog = fopen(['ErrorLog_',ErrLogName,'.txt'], 'a');
  26. % Check if directory or file:
  27. if isdir(OutputPath)
  28. OutFolder = OutputPath;
  29. else
  30. [OutFolder, ~, ~] = fileparts(OutputPath);
  31. end
  32. % Ensure directory exists:
  33. if ~exist(OutFolder, 'dir')
  34. [Status, ErrMsg] = mkdir(OutFolder);
  35. if Status == 0
  36. ErrMsg = sprintf(['ERROR: Failed to create output directory:'...
  37. '\n Reason: %s \n Folder: %s \n\n'], ErrMsg, OutFolder);
  38. fprintf(SaveErrLog, ErrMsg); % Record in ErrorLog
  39. error(ErrMsg);
  40. end
  41. end
  42. % Check write permissions on directory:
  43. [~, DirAttrib] = fileattrib(OutFolder);
  44. if DirAttrib.UserWrite ~= 1
  45. ErrMsg = sprintf(['ERROR: Do not have write permissions for output directory:'...
  46. '\n %s \n\n'], OutFolder);
  47. fprintf(SaveErrLog, ErrMsg); % Record in ErrorLog
  48. error(ErrMsg);
  49. end
  50. % If file specified and exists, check permissions on overwrite:
  51. if ~isdir(OutputPath) && exist(OutputPath, 'file')
  52. [~, FileAttributes] = fileattrib(OutputPath);
  53. if FileAttributes.UserWrite ~= 1
  54. ErrMsg = sprintf(['ERROR: Do not have write permissions to overwrite files:'...
  55. '\n %s \n\n'], OutputPath);
  56. fprintf(SaveErrLog, ErrMsg); % Record in ErrorLog
  57. error(ErrMsg);
  58. end
  59. end
  60. fclose(SaveErrLog);